package cn.dw.mm.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dw.mm.domain.NewEightSelf;
import cn.dw.mm.domain.Selfexamination;
import cn.dw.mm.mapper.SelfexaminationMapper;
import cn.dw.mm.service.SelfexaminationService;
import cn.dw.mm.utils.FileUpAndDown;

@Service
public class SelfexaminationServiceImpl implements SelfexaminationService{

	@Autowired
	private SelfexaminationMapper selfexaminationMapper;
	
	@Override
	public List<NewEightSelf> selectNewEightSelfsByEightFields(NewEightSelf newEightSelf) {
		List<Integer> ids = selfexaminationMapper.selectNewEightSelfsByEightFields(newEightSelf);//先查询出符合条件的工单id，再通过id查询对应的八大领域信息
		if (ids.size() != 0) {
			return selfexaminationMapper.selectNewEightSelfsByIds(ids);
		}
		return null;
	}

	@Override
	public int deleteById(String userId_selfExaminationId) {
		Integer selfExaminationId = Integer.valueOf(userId_selfExaminationId.split("_")[1]);
		String fileName = userId_selfExaminationId;
		selfexaminationMapper.deleteById(selfExaminationId);
		FileUpAndDown fud = new FileUpAndDown();
		if (fud.deleteFile(fileName, "E:/springUpload")) {
			return 1;
		} else {
			return -1;
		}
	}

	@Override
	public int insert(HttpServletRequest request) throws IllegalStateException, IOException {
		Integer userId = Integer.valueOf(Integer.parseInt(request.getParameter("userId")));
        String department = request.getParameter("userDepartment").toString(); 
		//构造实体类
		Selfexamination selfexamination =  new Selfexamination();
		selfexamination.setProblem(request.getParameter("problem"));
		selfexamination.setDiseducation(request.getParameter("diseducation"));
		selfexamination.setMeasures(request.getParameter("measures"));
		selfexamination.setFinish(request.getParameter("finish"));
		selfexamination.setFourforms(request.getParameter("fourforms"));
		selfexamination.setUserId(userId);
		selfexamination.setEightfieldsId(request.getParameter("eightfieldsId"));
		
		selfexaminationMapper.insert(selfexamination);
		int newDataId = selfexamination.getId();//插入操作完成后，mybatis会将新数据的id返回给Selfexamination实体类，直接getId获取即可
		if (newDataId != 0) {
			String fileName = userId.toString() + "_" + String.valueOf(newDataId);//用户id与工单id组成当前工单支撑材料的文件名
			selfexamination.setSupmaterial(fileName);
			selfexaminationMapper.update(selfexamination);
			FileUpAndDown fud = new FileUpAndDown();
			Boolean isUploadSuccess = fud.uploadFile(request, fileName, "E:/springUpload/" + department);
			if (isUploadSuccess) {
				return 1;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public int update(Selfexamination selfexamination) {
		return selfexaminationMapper.update(selfexamination);
	}
	
	@Override
	public List<Map<String, Object> > countByDepartment() {
		return selfexaminationMapper.countByDepartment();
	}

	@Override
	public List<Map<String, Object> > countByDomain() {
		return selfexaminationMapper.countByDomain();
	}
	
	@Override
	public List<Map<String, Object> > countByEightfield() {
		return selfexaminationMapper.countByEightfield();
	}

	@Override
	public Integer countByEightfieldIndex(Integer eightFieldsIndex) {
		return selfexaminationMapper.countByEightfieldIndex(eightFieldsIndex);
	}

	@Override
	public List<NewEightSelf> selectNewEightSelfsByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return selfexaminationMapper.selectNewEightSelfsByUserId(userId);
	}

	@Override
	public List<NewEightSelf> selectNewEightSelfsByUser(NewEightSelf newEightSelf) {
		return selfexaminationMapper.selectNewEightSelfsByUser(newEightSelf);
	}
	

}
